package cc.lpz.backend.controller;

import cc.lpz.backend.entity.User;
import cc.lpz.backend.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/user")
    public String query(){
        List<User> all = userMapper.findAll();
        System.out.println(all);
        return "查询用户";
    }
    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable int id){
        return "获取用户id信息";
    }

    @PostMapping("/user")
    public String save(User user){
        System.out.println("user = " + user);
        return "添加用户";
    }

    @PutMapping("/user")
    public String update(User user){
        System.out.println("user = " + user);
        return "更新用户";
    }

    @DeleteMapping("/user/{id}")
    public String delete(@PathVariable int id){
        System.out.println("id = " + id);
        return "删除用户";
    }

    @GetMapping("/useraddress/{id}")
    public String getUserAddressById(@PathVariable int id){
        return userMapper.selectById(id).toString();
    }

    @GetMapping("/user/find")
    public List<User> findByCond(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username","老王");
        return userMapper.selectList(userQueryWrapper);
    }
    @GetMapping("/user/findByPage")
    public IPage findByPage(){
        Page<User> page = new Page<>(0,2);
        Page<User> userPage = userMapper.selectPage(page, null);
        return userPage;
    }
}
